﻿using System;
using System.Collections.Generic;
using System.Linq;
using AmazeUI.bll.GetModel;
using AmazeUI.bll.Open.WeixinPay;
using AmazeUI.db;
using AmazeUI.Imodel;
using AmazeUI.Imodel.Res;
using WebCommon;

namespace AmazeUI.bll.Open
{
    public class PayBll : BaseBll
    {
        ///// <summary>
        ///// 添加一个支付记录
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns></returns>
        //public PayLog AddPayOrder(PayOrder model)
        //{
        //    var pl = new PayLog(model);
        //    //随即字符串
        //    pl.NonceStr = WebCommon.StringCommand.RandomsAzNum.GetLun(16);
        //    //时间戳
        //    pl.TimeStamp = WebCommon.StringCommand.Static.GetTimeStamp();


        //    this.db.PayLog.Add(pl);
        //    this.db.SaveChanges();
        //    return pl;
        //}




        /// <summary>
        /// 写入回调通知
        /// </summary>
        /// <param name="nm"></param>
        public bool SetNotify(INotifyPayLog nm, bool end = true)
        {

            var po = this.db.PayOrder.FirstOrDefault(a => a.OrderNo == nm.OrderNo);
            if (po == null)
            {
                return false;
            }
            if (po.IsPay)
            {
                //已经支付过了
                return false;
            }
            if (!po.IsEnd)
            {
                //订单无效
                return false;
            }
            po.IsEnd = end;
            po.IsPay = true;
            po.PayTime = DateTime.Now;
            po.PayBank = nm.PayBank;
            po.PayType = nm.PayTyp;
            po.PayTypeName = nm.PayTypeName;
            po.PartyOrderNo = nm.PartyOrderNo;
            this.db.SaveChanges();
            return true;
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="goodsId">商品id</param>
        /// <param name="ip">ip</param>
        /// <param name="adder">地址</param>
        /// <param name="dateTime">过期时间</param> 
        public PayOrder AddOrder(int goodsId, string ip, IAdder adder, DateTime dateTime)
        {
            var model = this.db.Goods.Find(goodsId);
            if (model.EndTime > DateTime.Now && model.IsEn && model.Num > 0 && model.EnTime < DateTime.Now)
            {
                --model.Num;

                var apo = new AddPayOrder(model, ip, adder, dateTime);
                var po = new PayOrder(apo);
                this.db.PayOrder.Add(po);
                this.db.SaveChanges();

                return po;

            }

            return null;
        }

        /// <summary>
        /// 获取订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PayOrder GetPayOrder(int id)
        {
            var po = this.db.PayOrder.Find(id);
            if (po.OverdueTime > DateTime.Now)
            {
                return po;
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 获取已付款报名订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IEnumerable<PayOrder2> Bmquery(int id)
        {
            var payOrders = this.db.PayOrder.Where(a => a.XtUserId == id).ToList().Select(WebCommon.StringCommand.Static.ParentToSub<PayOrder, PayOrder2>);
            return payOrders;
        }

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object PayOrderQuery(int id)
        {
            var payOrders = this.db.PayOrder.Where(a => a.XtUserId == id).ToList().Select(WebCommon.StringCommand.Static.ParentToSub<PayOrder, PayOrder2>);

            return payOrders;
        }

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public object PayOrderQuery(int rows, int page,string wd)
        {
            var payOrders = this.db.PayOrder.Where(a => a.OrderNo.Contains(wd) || a.Name.Contains(wd));
            PageListJs<PayOrder> pj = new PageListJs<PayOrder>(payOrders, rows, page);
            return pj.OpData(WebCommon.StringCommand.Static.ParentToSub<PayOrder, PayOrder2>);
 
        }

        /// <summary>
        /// 获取订单列表(分页)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public PageListJs<PayOrder2> PayOrderQuery(int id, int rows, int page)
        {
            var payOrders = this.db.PayOrder.Where(a => a.XtUserId == id);
            PageListJs<PayOrder> pj=new PageListJs<PayOrder>(payOrders,rows,page);
            return pj.OpData(WebCommon.StringCommand.Static.ParentToSub<PayOrder, PayOrder2>);
          
        }
    }
}